package com.company.jetpack.work;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

/**
 * author : dingqb
 * e-mail : dingqb@sany.com.cn
 * date   : 2020/11/20 11:39 AM
 * desc   :
 * version: 1.0
 */

public class ReadWorker extends Worker {
    public static final String TAG = "ReadWorker";
    public static final String PROGRESS = "PROGORESS";
    public ReadWorker(
            @NonNull Context context,
            @NonNull WorkerParameters params) {
        super(context, params);
    }

    @Override
    public Result doWork() {
        Log.e(TAG, "doWork: " + Thread.currentThread().getName());
        Data data=getInputData();
        // Do the work here--in this case, upload the images.
        try {
            read();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Indicate whether the work finished successfully with the Result
        return Result.success();
    }

    private void read() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Log.e(TAG, "read: i==" + i);
            // 更新进度
            setProgressAsync(new Data.Builder().putInt(PROGRESS, i).build());
            Thread.sleep(500);
        }
    }
}

